<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class OrderController extends Controller
{
    
    public function index()
    {
		//
    }
	
	//选择支付方式
	public function paycenter()
    {
		$money = $_GET['price']; //总价
		$freight = 0;   //运费
		if($money <59){
			$money = $money + 5;	//总价
			$freight = 5;   //运费
		}
		$stime = $_GET['stime'];   //送货时间
		$aid = $_GET['aid'];		//地址id
		$orderForm = time();		//订单号
		$payment = $_GET['payment']; //支付方式
		$ttime = date("Y-m-d H:i:s",time()); //生成订单时间 
		$orders = \DB::table("shopcar")->where("status",1)->where("uid",session("homeuser")->id)->get();
	
		//将商品添至订单表
		foreach($orders as $v){
			\DB::table("order")->insert(["bid"=>$v->id,"tname"=>$v->bookName,"logo"=>$v->bookPicture,"price"=>$v->price,"num"=>$v->number,"orderForm"=>$orderForm]);
			//添加后将购物车清除!!!!
			\DB::table("shopcar")->where("status",1)->where("uid",session("homeuser")->id)->delete();
		}
		\DB::table("order")->where("orderForm",$orderForm)->update(["uid"=>session("homeuser")->id,"stime"=>$stime,"payment"=>$payment,"ttime"=>$ttime,"money"=>$money,"freight"=>$freight,"aid"=>$aid]);
		$list = array($aid,$money,$stime,$ttime,$orderForm,$payment);
        return view("home/order/paycenter",["list"=>$list]);
    }
	
	
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
	//选择支付宝或者微信方式支付
    public function show($id)
    {
		$fu =  substr($id, 0, 1 );
		$orderForm = substr($id,1);
		$list = \DB::table("order")->where("orderForm",$orderForm)->first();
		if($fu == 1){
			return view("home/order/zhifubao",["list"=>$list]);
		}else{
			return view("home/order/weixin",["list"=>$list]);
		}
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
	//支付宝支付成功后
    public function edit($id)
    {
		//将状态改为已经付款
		\DB::table("order")->where("orderForm",$id)->update(["status"=>1]);
		$order = \DB::table("order")->where("orderForm",$id)->get();
		$id = $order[0]->aid;
		$addr = \DB::table("address")->where("id",$id)->first();
		//下句可能没有产生作用，因为本页paycenter方法
		$num1 = \DB::table("shopcar")->where("uid",session("homeuser")->id)->where("status",1)->get();
		$num = 0;
		foreach($num1 as $v){
			$num += $v->number;
		}
		
		
        return view("home/order/list",["order"=>$order,"addr"=>$addr,"num"=>$num]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
	
}
